﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using LinQ_DTO;

using System.Xml;

namespace TemplateH4
{
    public partial class DanhSachSanPham : System.Web.UI.Page
    {
        private string TenDSSP;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            String thm;
            thm = (string)Session["themeSelected"];
            if (thm != null)
            {
                Page.Theme = thm;
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/config.xml"));
                XmlNode root = doc.DocumentElement;
                String theme = root.SelectSingleNode("theme").ChildNodes[0].Value.Trim();
                Session.Add("themeSelected", theme);
                Page.Theme = theme;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            TenDSSP= Request.QueryString["TenDS"];
            BindData_grvSP(TenDSSP);
        }

        protected void BindData_grvSP(string TenDSSP)
        {

            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> ListSP = new List<shop_sanpham>();

            switch(TenDSSP)
            { 
                case "HotNam":
                    ListSP  = SpBus.LaySanPhamHotTheoPhai(true);
                    break;
                case "HotNu":
                    ListSP = SpBus.LaySanPhamHotTheoPhai(false);
                    break;
                case "KMNam":
                    ListSP = SpBus.LaySanPhamKhuyenMaiTheoPhai(true);
                    break;
                case "KMNu":
                    ListSP = SpBus.LaySanPhamKhuyenMaiTheoPhai(false);
                    break;
                case "Nam":
                    ListSP = SpBus.LaySanPhamTheoPhai(true);
                    break;
                case "Nu":
                    ListSP = SpBus.LaySanPhamTheoPhai(true);
                    break;
              
            }

            this.grvSP.DataSource = ListSP;
            grvSP.DataKeyNames = new string[] { "MaSanPham" };
            grvSP.DataBind();

            this.dtvSP.DataSource = ListSP;
            dtvSP.DataKeyNames = new string[] { "MaSanPham" };
            dtvSP.DataBind();

        }

        //Dung de bound du lieu vao cac field phuc tap: Image, Khuyen Mai
        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                shop_sanpham Sp = (shop_sanpham)e.Row.DataItem;//(shop_sanpham)ListSP[e.Row.];

                //bind image
                Image ImgControl = (Image)e.Row.FindControl("imgSP");
                ImgControl.AlternateText = Sp.HinhSanPham.Element("HinhTruoc").Value;
                ImgControl.ImageUrl = Sp.HinhSanPham.Element("HinhTruoc").Value;

                //bind Khuyen Mai
                Label KM = (Label)e.Row.FindControl("lbKM");
                string NoiDungKM = string.Empty;
                foreach (shop_sanpham_khuyenmai Km in Sp.shop_sanpham_khuyenmais)
                {
                    NoiDungKM += Km.shop_khuyenmai.NoiDung ;
                }
                if (NoiDungKM == "")
                    KM.Text = "Không";
                else
                    KM.Text = NoiDungKM;

            }
        }


        protected void grvSP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvSP.PageIndex = e.NewPageIndex;
            grvSP.EditIndex = -1;
            grvSP.SelectedIndex = -1;
        }

        protected void grvSP_PageIndexChanged(object sender, EventArgs e)
        {
            BindData_grvSP(TenDSSP);
        }

        protected void dtvSP_ItemCreated(object sender, EventArgs e)
        {
           
        }

        protected void dtvSP_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
           
        }



    }
}
